package com.horsen.service.aliyun.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.horsen.common.util.ResultCodeEnum;
import com.horsen.service.aliyun.service.VideoService;
import com.horsen.service.aliyun.utils.AliyunVodSdkUtils;
import com.horsen.service.aliyun.utils.VODProperties;
import com.horsen.service.base.exception.HseduException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * 视频 服务类
 *
 * @author Horsen
 */
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VODProperties vodProperties;

    /**
     * 上传视频至阿里云
     *
     * @param inputStream      输入流
     * @param originalFilename 原始文件名
     * @return 文件url地址
     */
    @Override
    public String upload(InputStream inputStream, String originalFilename) {
        String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        UploadStreamRequest request = new UploadStreamRequest(
                vodProperties.getKeyid(),
                vodProperties.getKeysecret(),
                title, originalFilename, inputStream);

        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);

        String videoId = response.getVideoId();
        // 没有正确的返回videoid则说明上传失败
        if (StringUtils.isEmpty(videoId)) {
            log.error("视频上传阿里云失败：" + response.getCode() + " - " + response.getMessage());
            throw new HseduException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        }
        return videoId;
    }

    /**
     * 从阿里云VOD删除视频
     *
     * @param videoId 视频ID
     * @throws ClientException 客户端异常
     */
    @Override
    public void deleteVideo(String videoId) throws ClientException {
        // 初始化Client
        DefaultAcsClient client = AliyunVodSdkUtils.initVodClient(vodProperties.getKeyid(), vodProperties.getKeysecret());
        // 创建请求对象
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(videoId);
        // 获取响应
        client.getAcsResponse(request);
    }

    /**
     * 获得视频播放凭证
     *
     * @param videoSourceId 视频文件ID
     * @return 播放凭证
     * @throws ClientException 客户端异常
     */
    @Override
    public String getPlayAuth(String videoSourceId) throws ClientException {
        // 初始化Client
        DefaultAcsClient client = AliyunVodSdkUtils.initVodClient(vodProperties.getKeyid(), vodProperties.getKeysecret());
        // 创建请求对象
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoSourceId);
        // 获取响应 得到凭证
        return client.getAcsResponse(request).getPlayAuth();
    }
}
